package com.mm.cloth.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class SlideCompareView extends View {
    
    private Bitmap originalBitmap;
    private Bitmap processedBitmap;
    private Paint paint;
    private Paint linePaint;
    
    private float slidePosition = 0.5f; // 滑动位置，0.0-1.0
    private RectF imageRect;
    private boolean isDragging = false;
    
    private static final int SLIDER_WIDTH = 4; // 滑动条宽度
    private static final int SLIDER_HANDLE_RADIUS = 20; // 滑动手柄半径
    
    public SlideCompareView(Context context) {
        super(context);
        init();
    }
    
    public SlideCompareView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }
    
    public SlideCompareView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }
    
    private void init() {
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setFilterBitmap(true);
        
        linePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        linePaint.setColor(0xFFFFFFFF); // 白色滑动线
        linePaint.setStrokeWidth(SLIDER_WIDTH);
        linePaint.setShadowLayer(4, 0, 0, 0x80000000); // 阴影效果
        
        imageRect = new RectF();
    }
    
    public void setOriginalBitmap(Bitmap bitmap) {
        this.originalBitmap = bitmap;
        calculateImageRect();
        invalidate();
    }
    
    public void setProcessedBitmap(Bitmap bitmap) {
        this.processedBitmap = bitmap;
        invalidate();
    }
    
    private void calculateImageRect() {
        if (originalBitmap == null) return;
        
        float viewWidth = getWidth();
        float viewHeight = getHeight();
        float imageWidth = originalBitmap.getWidth();
        float imageHeight = originalBitmap.getHeight();
        
        if (viewWidth == 0 || viewHeight == 0) return;
        
        float scale = Math.min(viewWidth / imageWidth, viewHeight / imageHeight);
        
        float scaledWidth = imageWidth * scale;
        float scaledHeight = imageHeight * scale;
        
        float left = (viewWidth - scaledWidth) / 2;
        float top = (viewHeight - scaledHeight) / 2;
        
        imageRect.set(left, top, left + scaledWidth, top + scaledHeight);
    }
    
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        calculateImageRect();
    }
    
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        
        if (originalBitmap == null) {
            // 绘制提示文字
            Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
            textPaint.setColor(0xFF666666);
            textPaint.setTextSize(48f);
            textPaint.setTextAlign(Paint.Align.CENTER);
            canvas.drawText("请先上传图片", getWidth() / 2f, getHeight() / 2f, textPaint);
            return;
        }
        
        // 计算滑动分割线的X坐标
        float dividerX = imageRect.left + imageRect.width() * slidePosition;
        
        // 绘制原图（左侧部分）
        canvas.save();
        canvas.clipRect(imageRect.left, imageRect.top, dividerX, imageRect.bottom);
        canvas.drawBitmap(originalBitmap, null, imageRect, paint);
        canvas.restore();
        
        // 绘制处理后的图片（右侧部分）
        if (processedBitmap != null) {
            canvas.save();
            canvas.clipRect(dividerX, imageRect.top, imageRect.right, imageRect.bottom);
            canvas.drawBitmap(processedBitmap, null, imageRect, paint);
            canvas.restore();
        }
        
        // 绘制分割线
        canvas.drawLine(dividerX, imageRect.top, dividerX, imageRect.bottom, linePaint);
        
        // 绘制滑动手柄
        Paint handlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        handlePaint.setColor(0xFFFFFFFF);
        handlePaint.setShadowLayer(6, 0, 2, 0x80000000);
        
        float handleY = imageRect.centerY();
        canvas.drawCircle(dividerX, handleY, SLIDER_HANDLE_RADIUS, handlePaint);
        
        // 绘制手柄内部图标（双箭头）
        Paint iconPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        iconPaint.setColor(0xFF666666);
        iconPaint.setStrokeWidth(3);
        
        // 左箭头
        Path leftArrow = new Path();
        leftArrow.moveTo(dividerX - 8, handleY);
        leftArrow.lineTo(dividerX - 12, handleY - 4);
        leftArrow.moveTo(dividerX - 8, handleY);
        leftArrow.lineTo(dividerX - 12, handleY + 4);
        canvas.drawPath(leftArrow, iconPaint);
        
        // 右箭头
        Path rightArrow = new Path();
        rightArrow.moveTo(dividerX + 8, handleY);
        rightArrow.lineTo(dividerX + 12, handleY - 4);
        rightArrow.moveTo(dividerX + 8, handleY);
        rightArrow.lineTo(dividerX + 12, handleY + 4);
        canvas.drawPath(rightArrow, iconPaint);
        
        // 绘制标签
        drawLabels(canvas, dividerX);
    }
    
    private void drawLabels(Canvas canvas, float dividerX) {
        Paint labelPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        labelPaint.setColor(0xFFFFFFFF);
        labelPaint.setTextSize(32f);
        labelPaint.setTextAlign(Paint.Align.CENTER);
        labelPaint.setShadowLayer(4, 0, 0, 0x80000000);
        
        float labelY = imageRect.top + 40;
        
        // 左侧标签（原图）
        if (slidePosition > 0.1f) {
            float leftLabelX = imageRect.left + (dividerX - imageRect.left) / 2;
            canvas.drawText("原图", leftLabelX, labelY, labelPaint);
        }
        
        // 右侧标签（去水印后）
        if (slidePosition < 0.9f && processedBitmap != null) {
            float rightLabelX = dividerX + (imageRect.right - dividerX) / 2;
            canvas.drawText("去水印后", rightLabelX, labelY, labelPaint);
        }
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (originalBitmap == null) return false;
        
        float x = event.getX();
        float y = event.getY();
        
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                // 检查是否点击在图片区域内
                if (imageRect.contains(x, y)) {
                    isDragging = true;
                    updateSlidePosition(x);
                    return true;
                }
                break;
                
            case MotionEvent.ACTION_MOVE:
                if (isDragging) {
                    updateSlidePosition(x);
                    return true;
                }
                break;
                
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                isDragging = false;
                break;
        }
        
        return false;
    }
    
    private void updateSlidePosition(float x) {
        if (imageRect.width() == 0) return;
        
        // 将触摸位置转换为滑动位置（0.0-1.0）
        float newPosition = (x - imageRect.left) / imageRect.width();
        
        // 限制在有效范围内
        slidePosition = Math.max(0.0f, Math.min(1.0f, newPosition));
        
        invalidate();
    }
    
    public void resetToCenter() {
        slidePosition = 0.5f;
        invalidate();
    }
    
    public void showOriginalOnly() {
        slidePosition = 1.0f;
        invalidate();
    }
    
    public void showProcessedOnly() {
        slidePosition = 0.0f;
        invalidate();
    }
}
